//
//  OAuthViewController.swift
//  weibo项目
//
//  Created by mac on 16/1/12.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking
class OAuthViewController: UIViewController {

//    属性是webView
    let webView = UIWebView()
    
    @objc private func close(){
    
    dismissViewControllerAnimated(true, completion: nil)
    
    }
    
    override func loadView() {
//        在加载的时候把view设置成webView
        
        view = webView
//        使用webView的代理方法
        webView.delegate = self
        
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        设置导航条
    setNarBar()
//       加载网络页面
        loadOAuthPage()
        
    }

    private func setNarBar(){
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .Plain , target: self, action: "close")
        
//        点击自动填充按钮的时候,把账号自动输入
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充账号", style: .Plain , target: self, action: "defaultAccount")
    
    }
    
//    自动填充账号的方法的实现
 @objc private  func defaultAccount(){
    let jsString = "document.getElementById('userId').value='15565275217',document.getElementById('passwd').value='weibo123456'"
    webView.stringByEvaluatingJavaScriptFromString(jsString)
    
    }
    
    private func loadOAuthPage(){
    
//urlString
        let urlString = "https://api.weibo.com/oauth2/authorize?" + "client_id=" + client_id + "&redirect_uri=" + redirect_uri
        let url = NSURL(string: urlString)!
        let request = NSURLRequest(URL:url)
        webView.loadRequest(request)
        
    
    
    
    
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

   }
//switf 是把一类的协议方法放在一个扩展中,这样方便管理

extension  OAuthViewController:UIWebViewDelegate{

    func webViewDidStartLoad(webView: UIWebView) {
//        网络加载的页面显示出来
        SVProgressHUD.show()
    }
    func webViewDidFinishLoad(webView: UIWebView) {
//        网络加载的页面被销毁
        SVProgressHUD.dismiss()
    }
    
//    一般协议的返回值是bool的时候,true是正常显示的,flase是不正常的
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
//        截取code
//        
//        absoulte  是绝对的  把url转换成字符串
        print(request.URL?.absoluteString)
//        当urlString没有的时候返回false
        guard let urlString = request.URL?.absoluteString else{
//        当无法获取urlString的时候不加载页面
        return false
        }
//        contains包含
        if urlString.containsString("code="){
        print(urlString)
//        获取code
            guard let   quary = request.URL?.query else{
                
            return false
            
            }
            print(quary)
            
            let codeStr = "code="
           let code = (quary as NSString).substringFromIndex(codeStr.characters.count)
            print("-----",code)
            print(request.URL)
            
            
            loadAccessToken(code)
            return false
        }
        return true
    }
    func loadAccessToken(code:String){
    
    UserAccountModel().loadAccessToken(code)
    
    }
    
}